//
//  RegisterViewController.m
//  HomeFinder
//
//  Created by zhenguanqing on 16/2/26.
//  Copyright © 2016年 蒋永昌. All rights reserved.
//

#import "RegisterViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import <Masonry.h>

#define k_width self.view.bounds.size.width
#define k_height self.view.bounds.size.height


@interface RegisterViewController ()


@property(nonatomic,strong) UITextField *userTF;
@property(nonatomic,strong) UITextField *passwordTF;
@property(nonatomic,strong) UITextField *phoneTF;
@property(nonatomic,strong) UITextField *verifyTF;
@property(nonatomic,strong) UIButton *verifyButton;

@property(nonatomic,strong) UIButton *registerButton;


@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"注册新用户";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"Comment_Icon_Back"] style:UIBarButtonItemStylePlain target:self action:@selector(goBackAction:)];


    [self addViews];
    

    
}



-(void)addViews{

    //用户名
    self.userTF = [[UITextField alloc]init];
    self.userTF.placeholder = @"用户名";
    self.userTF.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.userTF];
    
    [self.userTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(k_width * 0.8);
        make.height.mas_equalTo(k_height * 0.055);
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view.mas_top).offset(k_height * 0.2);
        
    }];
    
    
    //密码
    self.passwordTF = [[UITextField alloc]init];
    self.passwordTF.placeholder = @"请输入密码";
    self.passwordTF.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.passwordTF];
    
    [self.passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        

        make.size.equalTo(self.userTF);
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view.mas_top).offset(k_height * 0.3);
        
        
    }];
    
    
    //手机号
    self.phoneTF = [[UITextField alloc]init];
    self.phoneTF.placeholder = @"手机号";
    self.phoneTF.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.phoneTF];
    
    [self.phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
       

        make.size.equalTo(self.userTF);
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view.mas_top).offset(k_height * 0.4);

        
    }];
    
    
    //验证码
    self.verifyTF = [[UITextField alloc]init];
    self.verifyTF.placeholder = @"验证码";
    self.verifyTF.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.verifyTF];
    
    [self.verifyTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.equalTo(self.userTF);
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view.mas_top).offset(k_height * 0.5);
        
    }];
    
    
    //获取验证码
    self.verifyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.verifyButton setTitle:@"点击获取验证码" forState:UIControlStateNormal];
    self.verifyButton.backgroundColor = [UIColor colorWithRed:0.357 green:0.397 blue:0.389 alpha:1.000];
    self.verifyButton.tintColor = [UIColor whiteColor];
    self.verifyButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.verifyTF addSubview:self.verifyButton];
    
    [self.verifyButton addTarget:self action:@selector(getVerifyAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.verifyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(k_width * 0.28, k_height * 0.035));
        make.right.equalTo(self.verifyTF.mas_right).offset(-10);
        make.centerY.equalTo(self.verifyTF);
        
    }];
    
    
    
    

    
    
    //注册按钮
    self.registerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.registerButton setTitle:@"注册" forState:UIControlStateNormal];
    self.registerButton.backgroundColor = [UIColor colorWithRed:0.373 green:0.551 blue:1.000 alpha:1.000];
    self.registerButton.tintColor = [UIColor whiteColor];
    [self.view addSubview:self.registerButton];
    
    [self.registerButton addTarget:self action:@selector(registerAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.equalTo(self.userTF).multipliedBy(0.9);
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view.mas_top).offset(k_height * 0.6);
        
    }];


}

// 获取验证码
-(void)getVerifyAction:(UIButton*)sender{
    

    AVUser *user = [AVUser user];
    user.username = self.userTF.text;
    user.password =  self.passwordTF.text;
    //    user.email = @"hang@leancloud.rocks";
    user.mobilePhoneNumber = self.phoneTF.text;
    NSError *error = nil;
    [user signUp:&error];
    NSLog(@"点击了%@",error);


}

// 注册
- (void)registerAction{
    
    [AVUser verifyMobilePhone:self.verifyTF.text withBlock:^(BOOL succeeded, NSError *error) {
        //验证结果
        NSLog(@"注册成功");
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注册成功" message:@"跳转登陆页面?" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self.navigationController popViewControllerAnimated:YES];

        }];
        
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        [alert addAction:action1];
        [alert addAction:action2];
        
        [self presentViewController:alert animated:YES completion:nil];
        
    }];

    
}


-(void)goBackAction:(UIBarButtonItem*)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
